package com.lry.crawlers.cartoon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lry.crawlers.cartoon.entity.Cartoon;
import com.lry.crawlers.cartoon.mapper.CartoonMapper;
import com.lry.crawlers.cartoon.service.CartoonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CartoonServiceImpl implements CartoonService {
    @Autowired
    CartoonMapper cartoonMapper;

    @Override
    public int setCartoon(Cartoon cartoon) {
        int insert = cartoonMapper.insert(cartoon);
        return insert;
    }


    @Override
    public int setCartoons(List<Cartoon> cartoons) {
        int i = cartoonMapper.insertBatchSomeColumn(cartoons);
        return i;
    }



    @Override
    public List<Integer> removeDuplicates(List<String> titles) {
        List<Integer> duplicates = new ArrayList<>();
        for(int i = 0;i<titles.size();i++){
            QueryWrapper wrapper  = new QueryWrapper();
            wrapper.eq("title",titles.get(i));
            Cartoon f = cartoonMapper.selectOne(wrapper);
            if(f!=null){
                duplicates.add(i);
            }
        }
        return  duplicates;
    }

    @Override
    public void setupdateTime(Cartoon cartoon) {
        QueryWrapper wrapper =new QueryWrapper();
        wrapper.eq("cartoon_id",cartoon.getCartoonId());
        cartoonMapper.update(cartoon,wrapper);
    }

    @Override
    public Boolean removeDuplicate(String s) {
        QueryWrapper wrapper  = new QueryWrapper();
        wrapper.eq("title",s);
        Cartoon f = cartoonMapper.selectOne(wrapper);
        return f==null ? false : true;
    }

}


